package shuzi.widget;

import java.lang.ref.SoftReference;
import java.util.HashMap;

import shuzi.data.Database;
import shuzi.util.Download;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;

import com.shuzi.offer.R;
import com.shuzi.offer.R.color;

public class BrandImageView extends ImageView {

	private Handler handler = null;
	private Bitmap bitmap = null;
	private final int HANDLER_REFRSH_IMAGE = 0;
	private final int HANDLER_REFRSH_IMAGE_ERROR = HANDLER_REFRSH_IMAGE + 1;
	private final int HANDLER_REFRSH_IMAGE_ERROR2 = HANDLER_REFRSH_IMAGE_ERROR + 1;
	private MyThread thread = null;
	private int nowPosition = 0;
	private Context context;
	private Bitmap defult;

	public BrandImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.context = context;
		defult = getDefultImg();
		initHandler();

	}

	public BrandImageView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
		// TODO Auto-generated constructor stub
	}

	public BrandImageView(Context context) {
		this(context, null);
	}

/*	***
	 * 初始化handler
	 */
	private void initHandler() {
		handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				switch (msg.what) {
				case HANDLER_REFRSH_IMAGE:
					if(msg.arg1==nowPosition){
						setScaleType(ScaleType.FIT_XY);
						setImageBitmap(bitmap);
					}else{
						Log.v(Database.TAG, "当前位置为"+nowPosition+"需要显示的为"+msg.arg1);
					}
					break;
				case HANDLER_REFRSH_IMAGE_ERROR:
					setScaleType(ScaleType.FIT_XY);
					setImageDrawable(new ColorDrawable(color.lightgreen));
					break;
				case HANDLER_REFRSH_IMAGE_ERROR2:
					setScaleType(ScaleType.FIT_XY);
					setImageBitmap(defult);
					
					break;
				default:
					break;
				}

			}
		};

	}

	/**
	 * 从网上拿图
	 * 
	 * @param url
	 * @param keyName
	 *//*
	private void getBitmap(final String url, final String keyName) {
		new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					nowKeyName = keyName;
					byte[] data = Download.getImage(url);
					BitmapFactory.Options options = new BitmapFactory.Options();
					options.inPurgeable = true;
					options.inInputShareable = true;
					bitmap = BitmapFactory.decodeByteArray(data, 0,
							data.length, options);
					Database.mLinkedHashMap.put(keyName,
							new SoftReference<Bitmap>(bitmap));
					Log.v(Database.TAG, "keyName " + keyName + " 下载");
					handler.sendEmptyMessage(HANDLER_REFRSH_IMAGE);
				} catch (Exception e) {
					handler.sendEmptyMessage(HANDLER_REFRSH_IMAGE_ERROR);
					e.printStackTrace();
				}

			}
		}).start();

	}*/
	
	/**
	 * 返回默认的bitmap
	 * @return
	 */
	private Bitmap getDefultImg(){
		BitmapFactory.Options opts = new  BitmapFactory.Options();
		opts.inPurgeable = true;
		opts.inInputShareable = true;
		Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.phone_defult, opts);
		return bitmap;
	}

	/**
	 * 设置图片的背景
	 * 
	 * @param url
	 */
	public void setImageViewBg(String url, String keyName,int position,int type) {
		
		//如果为空，返回默认的图片
		if(url.equals("")){
			Message msg = new Message();
			if(type==Database.PHONE_INFO){
			msg.what = HANDLER_REFRSH_IMAGE_ERROR2;
			}else{
				msg.what = HANDLER_REFRSH_IMAGE_ERROR;
			}
			msg.arg1 = position;
			handler.sendMessage(msg);
			return;
		}
		
		nowPosition = position;
		
		if(thread!=null){
		//	Log.v(Database.TAG, "keyName = "+keyName+" 停止上一次线程");
			thread.interrupt();
			if(type==Database.PHONE_INFO){
				setScaleType(ScaleType.FIT_XY);
				setImageBitmap(defult);
			}else{
				setImageDrawable(new ColorDrawable(color.lightgreen));
			}
		}
		
		// 如果内存缓存为空
		if (Database.mLinkedHashMap == null) {
			Database.mLinkedHashMap = new HashMap<String, SoftReference<Bitmap>>();
		}

		// 如果包含了key
		if (Database.mLinkedHashMap.containsKey(keyName)) {
			SoftReference<Bitmap> softReference = Database.mLinkedHashMap
					.get(keyName);
			bitmap = softReference.get();
			// 如果不为空的话，直接使用
			if (bitmap != null) {
				// Log.v(Database.TAG, "keyName "+keyName+" 不为空");
				Message msg = new Message();
				msg.what = HANDLER_REFRSH_IMAGE;
				msg.arg1 = position;
				handler.sendMessage(msg);
			} else {
				Database.mLinkedHashMap.remove(keyName);
				//getBitmap(url, keyName);
				thread = new MyThread(url, keyName,position);
				thread.start();
			}
		} else { // 如果没有包含key
		//	getBitmap(url, keyName);
			thread = new MyThread(url, keyName,position);
			thread.start();
		}
	}

	class MyThread extends Thread {
		String url;
		String keyName;
		int position;
		

		public MyThread(String url, String keyName,int position) {
			this.keyName =keyName;
			this.url = url;
			this.position = position;
			
		}

		@Override
		public void run() {
			super.run();
			try {
				//nowKeyName = keyName;
				byte[] data = Download.getImage(url);
				BitmapFactory.Options options = new BitmapFactory.Options();
				options.inPurgeable = true;
				options.inInputShareable = true;
				bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,
						options);
				Database.mLinkedHashMap.put(keyName, new SoftReference<Bitmap>(
						bitmap));
			//	Log.v(Database.TAG, "keyName " + keyName + " 下载");
				Message msg = new Message();
				msg.what = HANDLER_REFRSH_IMAGE;
				msg.arg1 = position;
				handler.sendMessage(msg);
				//handler.sendEmptyMessage(HANDLER_REFRSH_IMAGE);
			} catch (Exception e) {
				handler.sendEmptyMessage(0);
				e.printStackTrace();
			}
		}
	}

}
